import { reactive, ref } from 'vue'

export function useForm() {
  const initialValues = {
    code: null as string | null,
    name: null as string | null,
    fullName: null as string | null,
    brandProviderCode: [] as string[] | null,
    productUniqueCode: null as string | null,
    specificationNumber: null as number | null,
    specificationName: null as string | null,
    packingNumber: null as number | null,
    packingName: null as string | null,
    shelfLife: null as number | null,
    shelfLifeUnit: null as number | null,
    originalPrice: null as number | null,
    actualPrice: null as string | null,
    supplyPrice: null as number | null,
    description: null as string | null
  }

  const state = reactive({
    detailFormRef: ref(),
    detailForm: { ...initialValues }
  })

  const meta = {
    model: state.detailForm,
    rules: {
      name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
      shelfLife: [{ required: true, message: '保质期不能为空', trigger: 'blur' }]
    }
  }

  const initForm = () => {
    state.detailForm = { ...initialValues }
  }

  const resetForm = () => {
    state.detailForm = { ...initialValues }
  }

  const getFormValues = () => state.detailForm

  const setDetail = (record: any) => {
    state.detailForm = { ...record }
  }

  return {
    meta,
    state,
    setDetail,
    initForm,
    resetForm,
    getFormValues
  }
}
